package com.example.navigation.viewModel

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.navigation.api.statistic.RoadRank
import com.example.navigation.api.statistic.StatisticRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Created with IntelliJ IDEA.
 * @Author: Liao Run
 * @Date: 2024-06-12 9:16
 * @Description:
 **/
@HiltViewModel
class StatisticsViewModel @Inject constructor(
    private val statisticRepository: StatisticRepository,
    @ApplicationContext val context: Context
) : ViewModel() {

    val roadRank = MutableStateFlow<RoadRank>(emptyArray())

    fun getRoadPmStatisticRank(startTime: String, endTime: String, qy: String) {

        viewModelScope.launch {
            val res =
                statisticRepository.getRoadPmStatisticRank(startTime, endTime, qy)

            if (res.code == 1) {
                roadRank.value = res.data?.records ?: emptyArray()
            } else {
                Toast.makeText(context, res.msg, Toast.LENGTH_SHORT).show()
            }
        }
    }
}